package com.yg.controller;

import com.yg.config.result.R;
import com.yg.entity.readAndWriteTemplates.TeachingMeetRead;
import com.yg.entity.singleEntity.TeachingMeet;
import com.yg.service.TeachingMeetService;
import com.yg.utils.CloseStream;
import com.yg.utils.DownLoadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * <p>
 * 对应sheet11，此表是教师参加教学会议表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/teachingMeet")
@CrossOrigin
public class TeachingMeetController {

    @Autowired
    private TeachingMeetService teachingMeetService;

    /**
     * 读取excel写入数据库
     *
     * @param file
     * @return
     */
    @PostMapping("/readExcel")
    public R readExcel(MultipartFile file) {
        //获取fileInputStream
        FileInputStream fileInputStream = null;
        R r = null;
        try {
            fileInputStream = (FileInputStream) file.getInputStream();
            r = teachingMeetService.readExcel(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            CloseStream.closeFileInputStream(fileInputStream);
        }
        return r;
    }

    /**
     * 导出excel
     *
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @GetMapping("/writeExcel")
    public R writeExcel(HttpServletResponse response, @RequestParam(required = false) String name) throws UnsupportedEncodingException {
        TeachingMeet teachingMeet = new TeachingMeet();
        teachingMeet.setName(name);
        R r = teachingMeetService.writeExcel(teachingMeet,response);
        return r;
    }

    /**
     * 条件查询
     *
     * @param
     * @return
     */
    @PostMapping("/selectTeachingMeetByCondition")
    public R teachingMeetConditionalQuery(String current, String limit, @RequestBody(required = false) TeachingMeetRead teachingMeetRead) {
        R r = null;
        if (teachingMeetRead != null) {
            r = teachingMeetService.conditionalQuery(current, limit, teachingMeetRead);
        } else {
            r = teachingMeetService.selectAllTeachMeet(current, limit);
        }

        return r;
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/getTeachingMeet")
    public R selectAllCourseBuild(String current, String limit) {
        R r = teachingMeetService.selectAllTeachMeet(current, limit);
        return r;
    }

    /**
     * 根据id删除教师信息
     *
     * @param
     * @return
     */
    @DeleteMapping("/deleteTeachingMeet")
    public R deleteteachingMeetById(@RequestBody TeachingMeet teachingMeet) {
        System.out.println("---------------------" + teachingMeet);
        R r = teachingMeetService.deleteTeachingMeetById(teachingMeet);
        return r;
    }

    /**
     * 根据id更新课程信息
     *
     * @param
     * @return
     */
    @PutMapping("/putTeachingMeet")
    public R updateCourseBuildById(@RequestBody TeachingMeetRead teachingMeetRead) {
        R r = teachingMeetService.updateTeachingMeet(teachingMeetRead);
        return r;
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @PostMapping("/postTeachingMeet")
    public R insertteachingMeet(@RequestBody TeachingMeet teachingMeet) {
        R r = teachingMeetService.insertTeachingMeet(teachingMeet);
        return r;
    }

}

